//
//  WXCategoryViewController.m
//  CarJX
//
//  Created by jxcl on 14-9-25.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "WXCategoryViewController.h"
#import "UIButton+image.h"
//#import "aliPaytestViewController.h"
#import "WXCarAroundViewController.h"
#import "CarAroundDataSource.h"
#import "WXCategoryButton.h"
#import "WXCarAroundViewController.h"
#import "CarServiceTableViewController.h"

#define MainScreen [UIScreen mainScreen].bounds

@interface WXCategoryViewController ()
{
    CarAroundDataSource *carAround;
    int _cellNumber;  //目录选项的个数
    int viewY;       //头视图的高度累加次数
    int moveViewy;   //头视图每次累加的高度
    UIView *_yiJiLeiMuView;
    NSArray *arr;  //类目数据
    NSArray *titleArr;
    NSMutableArray *rowHeightArray;   //用于存row的高度
    NSArray *imageArray;
    UITableView *_detailTabelView;
    
}

@end

@implementation WXCategoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *mobanView = [[UIView alloc] initWithFrame:self.view.bounds];
    mobanView.backgroundColor = [UIColor whiteColor];
    titleArr = [NSArray arrayWithObjects:@"保养",@"洗车",@"维修",@"美食",@"美食",@"酒店",@"娱乐",@"服务", nil];
    imageArray = [NSArray arrayWithObjects:@"atm_decoration",@"atm_maintenance_car",@"atm_repair_car",@"atm_foot",@"atm_foot",@"atm_hotel",@"atm_entertainment",@"atm_service", nil];
    [self setUp];
    rowHeightArray = [[NSMutableArray alloc] init];
    _yiJiLeiMuView = [[UIView alloc] init];
    _detailTabelView = [[UITableView alloc] init];
    _detailTabelView.delegate = self;
    _detailTabelView.dataSource = self;
    _detailTabelView.hidden = YES;
     [mobanView addSubview:_yiJiLeiMuView];
    [mobanView addSubview:_detailTabelView];
    [self.view addSubview:mobanView];
    [self getCellNumber];
}

- (void)setUp{
    self.title = @"周边搜索";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:@selector(dismiss) andCustom:NO];
}

-(void)dismiss{

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - 请求类目的所有数据
-(void)getCellNumber{
    [self.view makeToastActivity];
    if (carAround == nil) {
        carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    [carAround getServicePoint:^(id result) {
        arr = [result customObjectForkey:@"service_other"];
        titleArr = [result customObjectForkey:@"service_main"];
        [self initWithDataSource];
    } anfFail:^(id result) {
        
    }];
}

- (void)initWithDataSource {
    
    [self CreateViewWithNumbel:(int)[titleArr count]];
    _yiJiLeiMuView.frame = CGRectMake(0, 64,  MainScreen.size.width, viewY*moveViewy+100);
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, _yiJiLeiMuView.frame.size.height - 1, _yiJiLeiMuView.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [_yiJiLeiMuView addSubview:lineView];
    _detailTabelView.frame = CGRectMake(0,64+viewY*moveViewy+100 , MainScreen.size.width,MainScreen.size.height-(64+viewY*moveViewy+100));
    [self jiSuanRowHeight];    //计算每个row的高度，并存入数组rowheight
    [_detailTabelView reloadData];
    _detailTabelView.hidden = NO;
}

#pragma mark - 创建一级类目
-(void)CreateViewWithNumbel:(int)x{
    int y;
    if (x%4 == 0) {                      //将纪录四个一组，判断y坐标需要加几次
        y = x/4;
    }else{
        y = x/4+1;
    }
    for (int index2 = 0 ; index2 < y; index2++) {          //判断x坐标需要加几次
        for ( int index1 = 0; index1 < 4; index1++) {
            int temp = index2*4+index1;                    //判断当前到了第几个纪录
            if (temp < x) {
                [self creatButtonWithX:index1 andY:index2];
            }
        }
    }
}

-(void)creatButtonWithX:(int)x andY:(int)y{
    int temp = y*4+x;       //判断当前到了第几个纪录
    viewY = y;
    float moveX = 0;
    float moveY = 0;
    if([RextExtends getScreenFrame].size.width == 320) {
        moveX = 40+30;
        moveY = 40+20;
    }else if ([RextExtends getScreenFrame].size.width == 375) {
        moveX = 60+30;
        moveY = 50+20;
    }else if ([RextExtends getScreenFrame].size.width == 414) {
        moveX = 75+30;
        moveY = 60+20;
    }
    moveViewy = moveY;
    UILabel *namelabel = [[UILabel alloc] init];
    namelabel.frame = CGRectMake(35+x*moveX , 65+y*moveY, 40, 20);
    namelabel.font = [UIFont fontWithName:@"Helvetica" size:13];
    namelabel.text = [[titleArr objectAtIndex:temp] customObjectForkey:@"DIC_VALUE"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = temp+1;
    btn.frame = CGRectMake(30+x*moveX , 25+y*moveY, 40, 40);
    [btn addTarget:self action:@selector(categotyClick:) forControlEvents:UIControlEventTouchUpInside];
    ;
    
    if (!btn.currentImage ) {
    [btn setImage:[UIImage imageNamed:[imageArray objectAtIndex:temp]] forState:UIControlStateNormal];
     }
    [_yiJiLeiMuView addSubview:btn];
    [_yiJiLeiMuView addSubview:namelabel];
}


#pragma mark - 一级类目的点击事件
-(void)categotyClick:(UIButton *)sender{
    
    CarServiceTableViewController *carService;
    
    if (sender.tag != 4) {
        switch (sender.tag) {
            case 1:{
                carService = [[CarServiceTableViewController alloc] init:@"jzxc" andIsSendMessegeIsCategory:YES];
            }//jzxc
                break;
                
            case 2:{
                carService = [[CarServiceTableViewController alloc] init:@"upkeep" andIsSendMessegeIsCategory:YES];
            }
                break;
            case 3:{
                carService = [[CarServiceTableViewController alloc] init:@"weixiu" andIsSendMessegeIsCategory:YES];

            }
                break;
            default:
                break;
        }
        [self.navigationController pushViewController:carService animated:YES];
        
    }else{
        UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        WXCarAroundViewController *car = [stroy instantiateViewControllerWithIdentifier:@"carAround"];
        car.keyWord = @"美食";
        car.Navtitle = @"车周边";
        car.keyString1 = @"cate";
        [self.navigationController pushViewController:car animated:YES];
    }

}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   	UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                   reuseIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    NSString *DIC_VALUE = [dic objectForKey:@"DIC_VALUE"];
    
    [self createLabelWithindexPath:indexPath andCell:cell];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return 500;
}



#pragma mark - 创建二级类目
-(void)createLabelWithindexPath:(NSIndexPath *)indexPath andCell:(UITableViewCell *)cell{
//    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
//    NSArray *seq = [dic objectForKey:@"SEQ"];
    int numbel = arr.count;
    
    int y;
    if (numbel%3 == 0) {                      //将纪录三个一组，判断y坐标需要加几次
        y = numbel/3;
    }else{
        y = numbel/3+1;
    }
    for (int index2 = 0 ; index2 < y; index2++) {          //判断x坐标需要加几次
        for ( int index1 = 0; index1 < 3; index1++) {
            int temp = index2*3+index1;                    //判断当前到了第几个纪录
            if (temp < numbel) {
                [self createLabelwithX:index1 andY:index2 andCell:cell andSEQ:nil];
            }
        }
    }
    
    
}

-(void)createLabelwithX:(int)x andY:(int)y andCell:(UITableViewCell *)cell andSEQ:(NSArray *)seq{
    int temp = y*3+x;                           //判断当前到了第几个纪录
    NSDictionary * infodic = [arr objectAtIndex:temp];
    float moveX = 0;// 宽度，间隔
    float moveY = 0; //高度，间隔
    if([RextExtends getScreenFrame].size.width == 320) {
        moveX = 80+20;
        moveY = 20+10;
    }else if ([RextExtends getScreenFrame].size.width == 375) {
        moveX = 100+30;
        moveY = 25+10;
    }else if ([RextExtends getScreenFrame].size.width == 414) {
        moveX = 110+30;
        moveY = 25+10;
    }
    WXCategoryButton *btn = [WXCategoryButton buttonWithType:UIButtonTypeCustom];
    btn.tag = temp+1;
    btn.frame = CGRectMake(15+x*moveX, 20+y*moveY, 80, 20);
    [btn setTitle:[infodic objectForKey:@"DIC_VALUE"] forState:UIControlStateNormal];
    btn.dic_name = [infodic objectForKey:@"DIC_NAME"];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];
    [btn addTarget:self action:@selector(detailBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
    
}


#pragma mark - 二级目录的点击事件
-(void)detailBtnCLick:(WXCategoryButton *)sender{
    
    UIStoryboard *stroy = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WXCarAroundViewController *car = [stroy instantiateViewControllerWithIdentifier:@"carAround"];
    car.keyWord = sender.titleLabel.text;
    car.serverType = @"周边搜索";
    car.Navtitle = @"车周边";
    [self.navigationController pushViewController:car animated:YES];
    
}


#pragma mark - 计算row的高度
-(void)jiSuanRowHeight{
    for (int i = 0; i < arr.count; i++) {
        NSDictionary *dic = [arr objectAtIndex:i];
        NSArray *detailArray = [dic objectForKey:@"SEQ"];
        int x = detailArray.count;
        int y;
        if (x%3 == 0) {                      //将纪录四个一组，判断y坐标需要加几次
            y = x/3;
        }else{
            y = x/3+1;
        }
        float rowheight = y*30+35;    //  30 为标题的每个标题的高度，35为类目标题的高度
        [rowHeightArray addObject:[NSNumber numberWithFloat:rowheight]];
    }
    
    
    
}



@end
